﻿using System;
using System.Numerics;

namespace Beefly.Json.Extensions;

static class NumberExtension
{
    public static bool TryReadInt8Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = sbyte.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadUInt8Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = byte.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadInt16Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = short.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadUInt16Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = ushort.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadInt32Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = int.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadUInt32Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = uint.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadInt64Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = long.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadUInt64Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = ulong.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadInt128Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = Int128.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadUInt128Char<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = UInt128.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadBigIntChar<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = BigInteger.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadFloatChar<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = float.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
    
    public static bool TryReadDoubleChar<TTarget>(in this Span<char> str, out TTarget value)
    {
        var sourceNumber = double.Parse(str);
        if (sourceNumber is TTarget item)
        {
            value = item;
            return true;
        }

        value = default;
        return false;
    }
}